package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.CorpMember;
import com.whmskj.xjlhbc.service.IPersonnelManagementDataSheetsService;
import com.whmskj.xjlhsc.entity.Sb08tasksheet;
import com.whmskj.xjlhsc.entity.WbBj;
import com.whmskj.xjlhsc.mapper.Sb08tasksheetMapper;
import com.whmskj.xjlhsc.mapper.Yg01employeefilesMapper;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.dto.WbTzDto;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.WbListVo;
import com.whmskj.xjlhsc.service.Sb08tasksheetService;
import com.whmskj.xjlhsc.service.WbBjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
* @author i
* @description 针对表【sb08Tasksheet】的数据库操作Service实现
* @createDate 2025-08-29 08:52:00
*/
@Service
public class Sb08tasksheetServiceImpl extends ServiceImpl<Sb08tasksheetMapper, Sb08tasksheet>
    implements Sb08tasksheetService{
    @Autowired
    private Sb08tasksheetMapper sb08tasksheetMapper;
    @Autowired
    private WbBjService wbBjService;

    @Autowired
    private Yg01employeefilesMapper yg01employeefilesMapper;
    @Override
    public List<WbListVo> wblist(String process, Integer state, String type, String startTime, String endTime) {
        //TODO:获取登录用户的姓名
        CorpMember corpMember = getCorpMember();
        String nick = corpMember.getNick();
        Page.startPage();
        //TODO:返回维保备件
        List<WbListVo> wbListVoList = sb08tasksheetMapper.selectWbListVo(nick,process,state,type,startTime,endTime);
        return wbListVoList;
    }

    /**
     * 维保调整
     * @param wbTzDto
     */
    @Override
    public void wbtz(WbTzDto wbTzDto) {
        LambdaUpdateWrapper<Sb08tasksheet> sb08tasksheetLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        sb08tasksheetLambdaUpdateWrapper.eq(Sb08tasksheet::getId,wbTzDto.getId());
        sb08tasksheetLambdaUpdateWrapper.set(Sb08tasksheet::getEquipmentHangars,wbTzDto.getEquipmentHangars());
        sb08tasksheetLambdaUpdateWrapper.set(Sb08tasksheet::getMaintenanceType,wbTzDto.getMaintenanceType());
        sb08tasksheetLambdaUpdateWrapper.set(Sb08tasksheet::getPlannedDate,wbTzDto.getPlannedDate());
        boolean update = this.update(sb08tasksheetLambdaUpdateWrapper);
        Assert.isTrue(update,"维保调整失败");
    }

    /**
     * 添加维保
     * @param sb08tasksheet
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addwb(Sb08tasksheet sb08tasksheet) {
        boolean flag = false;
        //如果维保完成就有完成时间
        sb08tasksheet.setCompletionDate(new Date());
      flag =  this.saveOrUpdate(sb08tasksheet);
        List<WbBj> wbBjList = sb08tasksheet.getWbBjList();
        if (CollectionUtil.isNotEmpty(wbBjList)) {
            LambdaUpdateWrapper<WbBj> wbBjLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            wbBjLambdaUpdateWrapper.eq(WbBj::getWbId,sb08tasksheet.getId());
            wbBjService.remove(wbBjLambdaUpdateWrapper);
            //TODO:再次保存维保配件
            flag = wbBjService.saveBatch(wbBjList);
        }
       Assert.isTrue(flag,"保存维保失败");
    }

    /**
     * 临时保存维保
     * @param sb08tasksheet
     */
    @Override
    public void addwbTemp(Sb08tasksheet sb08tasksheet) {
        boolean flag = false;
        //如果维保完成就有完成时间
        flag =  this.saveOrUpdate(sb08tasksheet);
        List<WbBj> wbBjList = sb08tasksheet.getWbBjList();
        if (CollectionUtil.isNotEmpty(wbBjList)) {
            //TODO:再次保存维保配件
            LambdaUpdateWrapper<WbBj> wbBjLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            wbBjLambdaUpdateWrapper.eq(WbBj::getWbId,sb08tasksheet.getId());
            wbBjService.remove(wbBjLambdaUpdateWrapper);
            flag = wbBjService.saveBatch(wbBjList);
        }

        Assert.isTrue(flag,"临时保存维保失败");
    }

    /**
     * 维保审核
     * @param processStatus
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<WbListVo> wbsh(String processStatus, String startTime, String endTime) {
        //获取登录人的信息
        CorpMember corpMember = getCorpMember();
        String nick = corpMember.getNick();
        //获取登录人能审核的员工信息
       List<String> YgNames = yg01employeefilesMapper.selectByVoield(nick);
       Page.startPage();
     List<WbListVo>  wbListVos = sb08tasksheetMapper.selectWbListVo2(YgNames,processStatus,startTime,endTime);
        return wbListVos;
    }

    @Override
    public void addwbsh(Sb08tasksheet sb08tasksheet) {
        boolean flag = false;
        //获取登录人的信息
        CorpMember corpMember = getCorpMember();
        String nick = corpMember.getNick();
        sb08tasksheet.setApprovingOfficer(nick);
        sb08tasksheet.setAuditTime(new Date());
        //如果维保完成就有完成时间
        sb08tasksheet.setCompletionDate(new Date());
        flag =  this.saveOrUpdate(sb08tasksheet);
        List<WbBj> wbBjList = sb08tasksheet.getWbBjList();
        if (CollectionUtil.isNotEmpty(wbBjList)) {
            flag = wbBjService.saveBatch(wbBjList);
        }
        Assert.isTrue(flag,"保存维保失败");
    }

    private CorpMember getCorpMember(){
        IPersonnelManagementDataSheetsService personnel = SpringUtil.getBean(IPersonnelManagementDataSheetsService.class);
        return personnel.getCorpMemberByLogin();
    }
}




